﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyKho
{
    public partial class frm_HangHoa : Form
    {
        ConnectDB Db = new ConnectDB();
        DataGridViewRow row_dshh = new DataGridViewRow();


        public frm_HangHoa()
        {
            InitializeComponent();
        }

        public void showDSHangHoa()
        {
            string sql = "SELECT hh.MaHH, hh.TenHH, hh.NuocSX, hh.KichThuoc, hh.SLNhap, dvt.TenDVT, nhh.TenNhomHH, hh.MaNhomHH, hh.MaDVT " + 
                "FROM HangHoa hh, DonViTinh dvt, NhomHangHoa nhh  " +
                "WHERE hh.IsDeleted=1 and hh.MaDVT = dvt.MaDVT and hh.MaNhomHH = nhh.MaNHH ";
            DataView dv = new DataView(Db.getDataTable(sql));

            dgv_DSHangHoa.DataSource = dv; 
            dgv_DSHangHoa.Columns["MaDVT"].Visible = false;
            dgv_DSHangHoa.Columns["MaNhomHH"].Visible = false;

            //dgv_DSHangHoa.ClearSelection();
            
           
            //dgv_DSHangHoa.AutoResizeColumns();
        }

        private System.Data.DataView DataView(System.Data.DataTable dataTable)
        {
            throw new NotImplementedException();
        }
        //show du lieu lay dc khi chon 1 item trong DS hang hoa
        private void showDaTaRow(DataGridViewRow row)
        {
            txtMaHH.Text = row.Cells["MaHH"].Value.ToString();
            txtTenHH.Text = row.Cells["TenHH"].Value.ToString();
            txtNSX.Text = row.Cells["NuocSX"].Value.ToString();

            txtKichThuoc.Text = row.Cells["KichThuoc"].Value.ToString();
            txtSoLuong.Text = row.Cells["SLNhap"].Value.ToString();

            cmbNhomHH.SelectedValue = row.Cells["MaNhomHH"].Value;
            cmbDonViTinh.SelectedValue = row.Cells["MaDVT"].Value;
        }

        private void showTextbox()// f12 k dược
        {
            txtMaHH.Text = "đang khởi tạo...";
            txtSoLuong.Text = "0";
            txtTenHH.Text = null;
            txtKichThuoc.Text = null;
            txtNSX.Text = null;
            cmbDonViTinh.SelectedIndex = 0;
            cmbNhomHH.SelectedIndex = 0;
            showChucNang(false);
            
        }

        private void showChucNang(Boolean temp)
        {
            btnUpdate.Enabled = temp;
            btnDel.Enabled = temp;
        }


        private void frm_HangHoa_Load(object sender, EventArgs e)
        {
            showDonViTinh();
            showNhomHH();
            showDSHangHoa();
            showTextbox();
            showChucNang(false);
        }

        void showNhomHH()
        {
            string sql = "SELECT * FROM NhomHangHoa WHERE isDeleted=1";
            DataView dv = new DataView(Db.getDataTable(sql));
            cmbNhomHH.DataSource = dv;
            cmbNhomHH.DisplayMember = "TenNhomHH";// cái này tên cho mai chon
            cmbNhomHH.ValueMember = "MaNHH";
        }

        private void showDonViTinh()//uh, biet roi, vè cái đã, đang nhà à,
        {
            string sql = "SELECT * FROM DonViTinh WHERE isDeleted=1";
            DataView dv = new DataView(Db.getDataTable(sql));
            cmbDonViTinh.DataSource = dv;
            cmbDonViTinh.DisplayMember = "TenDVT";
            cmbDonViTinh.ValueMember = "MaDVT";
        }

        private void dgv_DSHangHoa_SelectionChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dgv_DSHangHoa.SelectedRows)
            {
                //string value1 = row.Cells[0].Value.ToString();
                //MessageBox.Show(value1);
                showDaTaRow(row);
                showChucNang(true);
                
            }
        }

        //private void dgv_DSHangHoa_CellClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    int rowIndex = e.RowIndex;
        //    //MessageBox.Show(rowIndex.ToString());
        //    if (rowIndex >= 0)
        //    {
        //        DataGridViewRow row = dgv_DSHangHoa.Rows[rowIndex];
        //        //string value1 = row.Cells[1].Value.ToString();
        //        //MessageBox.Show(value1);
        //        showDaTaRow(row);

        //    }
           
        //}

       

        //cap nhat lai hang hoa
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string maHH = txtMaHH.Text;
            string tenHH = txtTenHH.Text;
            string nsx = txtNSX.Text;
            string kt = txtKichThuoc.Text;
            string sl = txtSoLuong.Text;
            string nhomHH = cmbNhomHH.SelectedValue.ToString();
            string dvt = cmbDonViTinh.SelectedValue.ToString();

            string sql = " UPDATE HangHoa " +
                " SET TenHH =  N'" + tenHH + "' , NuocSX = N'" + nsx + "' , KichThuoc = N'" + kt +"'" +
                    " , SlNhap = " + sl + " , MaNhomHH = N'" + nhomHH + "' , MaDVT = N'" + dvt +"'" +
                " WHERE MaHH = N'" + maHH + "'";

            Db.ExecuteNonQuery(sql);
            MessageBox.Show("Update thành công !!!");
            showDSHangHoa();
            
        }

        //xoa hang hoa
        private void btnDel_Click(object sender, EventArgs e)
        {
            string maHH = txtMaHH.Text;

            DialogResult result = MessageBox.Show("Bạn có thực sự muốn xóa mã hàng hóa: " + maHH + "?", "Xóa hàng hóa", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                string sql = " UPDATE HangHoa " +
                " SET IsDeleted = 0" +
                " WHERE MaHH = N'" + maHH + "'";

                Db.ExecuteNonQuery(sql);
                MessageBox.Show("Xoa thành công !!!");
                showDSHangHoa();
            }
            else if (result == DialogResult.No)
            {
                //...
            }
            
        }

        //them hang hoa
        private void btnAdd_Click(object sender, EventArgs e)
        {
            
            if (txtMaHH.Text == "đang khởi tạo...")
            {
                //Str = Str.Trim() Cắt hết khoảng trắng ở đầu và cuối chuỗi 
                string tenHH = txtTenHH.Text.Trim();
                string nsx = txtNSX.Text.Trim();
                string kt = txtKichThuoc.Text.Trim();
                //string sl = txtSoLuong.Text;
                string sl = "0";
                string nhomHH = cmbNhomHH.SelectedValue.ToString();
                string dvt = cmbDonViTinh.SelectedValue.ToString();

                //string maHH = Db.GetLastID("HangHoa", "MaHH");
                string maHH = Db.GetLastIDNumber("HangHoa", "MaHH", "def0");
                //MessageBox.Show(maHH);

                //Str1 = Str.SubString(4):Tạo chuỗi con từ chuỗi Str bắt đầu từ vị trí 4 đến hết (bo doan def0)
                maHH = maHH.Substring(4);
                int temp = int.Parse(maHH);
                maHH = "def0" + (temp + 1).ToString();
                
                
                tenHH = tenHH.Trim();
                if (tenHH != "")
                {
                    string sql = "INSERT INTO HangHoa " +
                        " VALUES (N'" + maHH + "', N'" + tenHH + "', N'" + nsx + "', N'" + kt + "', N'" + nhomHH + "', N'" + dvt + "', " + sl +", 1)";
                    //MessageBox.Show(sql);
                    Db.ExecuteNonQuery(sql);
                    MessageBox.Show("Hàng hóa: " + tenHH + " được thêm thành công !!!");
                    showDSHangHoa();
                }
                else
                {
                    MessageBox.Show("Tên Hàng hóa không được để trống !!!");
                }
            }
            else
            {
                showTextbox();
                MessageBox.Show("Vui lòng điền mới thông tin hàng hóa !!!");
            }
            
            
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
 

    }
}
